"""
dropout简洁实现
"""
import tensorflow as tf
from d2l import tensorflow as d2l

if __name__== '__main__':
    dropout1, dropout2 = 0.2, 0.5
    num_epochs, lr, batch_size = 10, 0.5, 256
    net = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(256, activation=tf.nn.relu),
        # 在第一个全连接层之后添加一个dropout层
        tf.keras.layers.Dropout(dropout1),
        tf.keras.layers.Dense(256, activation=tf.nn.relu),
        # 在第二个全连接层之后添加一个drouput层
        tf.keras.layers.Dropout(dropout2),
        tf.keras.layers.Dense(10)
    ])
    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
    train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
    trainer = tf.keras.optimizers.SGD(learning_rate=lr)
    d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
    d2l.plt.show()